package com.zhuiyun.project.api.music.controller;

import com.zhuiyun.project.annotation.OptLog;
import com.zhuiyun.project.api.music.entity.SysMusic;
import com.zhuiyun.project.api.music.model.MusicModel;
import com.zhuiyun.project.api.music.service.MusicService;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.security.commonhandler.SecurityLoginUser;
import com.zhuiyun.project.security.commonhandler.SecurityLoginUserVerification;
import com.zhuiyun.project.utils.SecurityUtils;
import com.zhuiyun.project.utils.StringUtils;
import com.zhuiyun.project.utils.constants.CommonConstants;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;

/**
 * @ClassName MusicController
 * @Description TODO 音乐管理模块
 * @Author XMY
 * @Date 2023/12/6 15:44
 **/

@RestController
@RequestMapping("/music")
@Slf4j
public class MusicController {
    @Autowired
    private MusicService musicService;
    /**
    * @Author 音频列表分页查询
    * @Description
    * @Date 2023/12/6 18:58
    * @Return
    * @param sysMusic
    * @param pageRequest
    */
    @GetMapping("/getMusicList")
    @OptLog(modular = "音乐管理", type = CommonConstants.SELECT, describe = "查询诗词")
    @ApiOperation(value = "查询音频列表")
    public CommonResult getMusicList(SysMusic sysMusic, PageRequest pageRequest){
        log.info("查询");
        return CommonResult.ok(musicService.getMusicList(sysMusic, pageRequest));
    }
    /**
    * @Author XMY
    * @Description 音频上传
    * @Date 2023/12/6 19:00
    * @Return
    * @param file
    */
    @PostMapping("/audioUpload")
    @ApiOperation(value = "后台音频上传接口")
    public CommonResult audioUpload(@RequestParam MultipartFile file,String username)throws CommonException {
        log.info("后台音频上传");
        return musicService.audioUpload(file,username);
    }
    /**
    * @Author XMY
    * @Description 修改音频信息
    * @Date 2023/12/7 11:27
    * @Return
    * @param musicModel
    */
    @ApiOperation(value = "修改用户")
    @OptLog(modular = "音乐管理", type = CommonConstants.UPDATE, describe = "修改音频信息")
    @PutMapping("/updateMusic")
    public CommonResult edit(@Validated @RequestBody MusicModel musicModel)throws CommonException  {
        return CommonResult.ok(musicService.updateMusic(musicModel));
    }

    /**
     * @Author XMY
     * @Description 删除音频信息
     * @Date 2023/12/6 19:43
     * @Return
     * @param picId
     */
    @DeleteMapping("/delMusicById/{picId}")
    @OptLog(modular = "音乐管理", type = CommonConstants.DELETE, describe = "删除音频信息")
    @ApiOperation(value = "删除音频信息方法")
    public CommonResult delMusicByIds(@PathVariable Integer[] picId) throws CommonException {
        log.info("删除音频信息");
        return musicService.delMusicByIds(picId);
    }
    /**
    * @Author 获取登录用户信息
    * @Description
    * @Date 2023/12/8 9:06
    * @Return
    * @param request
    */
    @GetMapping("/getLoginUser")
    public String getUserList(HttpServletRequest request){
        SecurityLoginUser loginUser = SecurityLoginUserVerification.getLoginUser(request);
        String name = loginUser.getUsername();
        return name;
    }

}
